<template>
	<view class="container">
		<view class="top">
			<u-tabs :list="list" :is-scroll="false" :current="current" @change="change" lineColor='#8d4a32'  active-color='#8d4a32'></u-tabs>
		</view>
		<view v-if="nullInfo"
			class="d-flex w-100 h-100 flex-column just-content-center align-items-center">
			<image src="../../static/logo_shop.jpg" class="drinks-img"></image>
			<view class="tips d-flex flex-column align-items-center font-size-base text-color-assist">
				<view>您还没有点单</view>
				<view>快去犒劳一下自己吧~</view>
			</view>
			<button type="primary" class="drink-btn" size="default" @tap="menu">去点餐</button>
		</view>
		<view v-else>
			<view class="container">
				<view class="orders-list d-flex flex-column w-100" style="padding: 20rpx; padding-bottom: 0;">
					<view class="order-item" v-for="(item, index) in orderList" :key="index" style="margin-bottom: 30rpx;" @tap="detail(item.orderId)">
						<list-cell :hover="false">
							<view class="w-100 d-flex align-items-center">
								<view class="flex-fill d-flex flex-column">
									<view class="font-size-lg text-color-base" style="margin-bottom: 20rpx;">
										{{ item.shopName }}
									</view>
									<view class="font-size-sm text-color-assist">订单编号：{{ item.orderId }}</view>
								</view>
								<view class="font-size-lg text-color-primary statusBox">
									<text>{{ statusList[item.status] }}</text>
								</view>
							</view>
						</list-cell>
						<list-cell :hover="false" last>
							<view class="w-100 d-flex flex-column">
								<view class="w-100 text-truncate font-size-lg text-color-base foodTit" >
									<text v-for="(goods,index) in item.foodOrderDetailList" :key="index" >
										{{goods.foodName}}*{{goods.foodNumber}}
									</text>
								</view>
								<view class="d-flex justify-content-between align-items-center" style="margin-bottom: 30rpx;">
									<view class="font-size-sm text-color-assist">
										{{ item.completed_time }}
									</view>
									<view class="d-flex font-size-sm text-color-base align-items-center">
										<view style="margin-right: 10rpx;">共{{ totalNum(item.foodOrderDetailList) }}件餐品，实付</view>
										<view class="font-size-lg">￥{{ item.orderAmount }}</view>
									</view>
								</view>
							</view>
						</list-cell>
					</view>
				</view>
			</view>
			<view style="width: 100%;height: 60px;"></view>
			<drag-button
			            :isDock="true"
			            :existTabBar="true"
			            @btnClick="btnClick"
			             />
		</view>
	</view>
</template>

<script>
	import listCell from '@/components/list-cell/list-cell';
	import dragButton from "@/components/drag-button/drag-button.vue";
	export default {
		components: {
			listCell,dragButton
		},
		data() {
			return {
				orderList: [],
				list: [{
					name: '当前订单'
				}, {
					name: '历史订单'
				}, ],
				current: 0,
				pageNum:1,
				currentMax:0,
				status: 'none',
				bottom:false,
				nullInfo:false,
				statusList:[
					'待支付','待商家接单','已取消','商家已接单','商家制作完成','配送中','已送达','退款中','退款完成','同意部分退款','拒绝部分退款'
				],
				clickFlag:false,
				clickNum:0
				
			}
		},
		onReachBottom() {
			if (this.pageNum < this.currentMax) {
				this.pageNum++;
				this.status = 'loading'
				this.getData()
			} else {
				this.bottom = true;
				this.status = 'nomore'
		
			}
		},
		computed: {
			
		},
		onShow() {
			this.current = 0;
			this.pageNum = 1;
			this.orderList = [];
			this.getData();
		},
		onLoad() {
			this.$wxHide()
		},
		methods: {
			//刷新当前订单
			btnClick(){
				var that = this;
				if(that.clickNum>10){
					uni.showToast({
						icon:'none',
						title:'请勿频繁刷新~'
					})
					setTimeout(function() {
						that.clickNum = 0
					}, 1500);
				}else{
					if(that.clickFlag){
						
					}else{
						that.pageNum = 1;
						that.orderList = [];
						that.bottom = false;
						that.clickNum++;
						that.getData()
					}
				}
				
				
			},
			
			//订单详情
			detail(id) {
				uni.navigateTo({
					url: '/pages/orders/detail?id=' + id
				})
			},
			
			//计算总件数
			totalNum(item){
				var num = 0
				
				for (var i = 0; i < item.length; i++) {
					num = Number(item[i].foodNumber) + num
				}
				return num
			},
			//tab切换
			change(index) {
				this.current = index;
				this.pageNum = 1;
				this.orderList = [];
				this.bottom = false
				this.getData()
			},
			//加载订单列表
			getData(){
				uni.showLoading({
					title:'加载中'
				})
				var that = this;
				that.clickFlag = true;
				
				that.uni_request({
					url: '/api/order/list',
					method: 'POST',
					data:{
						pageNum: that.pageNum,
						pageSize: 10,
						isFinished:that.current
					},
					success: (res) => {
						uni.hideLoading()
						that.clickFlag = false
						if (res.data.code == 0 || res.data.code == 200) {
							that.currentMax = res.data.data.totalPage;
							var data = res.data.data.list;
							for (var i = 0; i < data.length; i++) {
								that.orderList.push(data[i]);
							}
							
							if (that.orderList.length == 0) {
								that.nullInfo = true
							} else {
								that.nullInfo = false
							}
						
						} else {
							that.nullInfo = true
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							})
						}
					},
					fail(e){
						uni.hideLoading()
					},
				})
			},
			
			
			//更新订单状态
			refreshOrder() {

			},
			orders() {

				uni.navigateTo({
					url: '/pages/orders/orders'
				})
			},
			menu() {
				uni.switchTab({
					url: '/pages/index/index'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	/* #ifdef H5 */
	page {
		min-height: 100%;
		// background-color: $bg-color;
		background: #f5f2f0 !important;
	}

	/* #endif */
	.order-box {
		padding: 20rpx;
		/* #ifdef H5 */
		margin-bottom: 100rpx;
		/* #endif */
	}
	.foodTit{
		margin-bottom: 30rpx;
		text{
			margin-right: 10px;color: $base;font-weight: bold;
		}
	}
	.refresh {
		height: 28px;
		width: 28px;
	}

	.drinks-img {
		width: 260rpx;
		height: 260rpx;
	}

	.tips {
		margin: 60rpx 0 80rpx;
		line-height: 48rpx;
	}

	.drink-btn {
		width: 320rpx;
		border-radius: 50rem !important;
		margin-bottom: 40rpx;
		font-size: $font-size-base;
		line-height: 3.0;
	}

	@mixin arch {
		content: "";
		position: absolute;
		background: rgba(245, 242, 240, 1) !important;
		width: 30rpx;
		height: 30rpx;
		bottom: -15rpx;
		z-index: 10;
		border-radius: 100%;
	}

	.section {
		position: relative;

		&::before {
			@include arch;
			left: -15rpx;
		}

		&::after {
			@include arch;
			right: -15rpx;
		}
	}
	.zw{
		height: 50px;width: 100%;
	}
	.pay-cell {
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: $font-size-base;
		color: $text-color-base;
		margin-bottom: 40rpx;

		&:nth-last-child(1) {
			margin-bottom: 0;
		}
	}

	.sort-num {
		font-size: 64rpx;
		font-weight: bold;
		color: $text-color-base;
		line-height: 2;
	}

	.steps__img-column {
		display: flex;
		margin: 30rpx 0;

		.steps__img-column-item {
			flex: 1;
			display: flex;
			align-items: center;
			justify-content: center;

			image {
				width: 80rpx;
				height: 80rpx;
			}
		}
	}

	.steps__text-column {
		display: flex;
		margin-bottom: 40rpx;

		.steps__text-column-item {
			flex: 1;
			display: inline-flex;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: $font-size-base;
			color: $text-color-assist;

			&.active {
				color: $text-color-base;
				font-weight: bold;

				.steps__column-item-line {
					background-color: $text-color-base;
				}
			}

			.steps__column-item-line {
				flex: 1;
				height: 2rpx;
				background-color: #919293;
				transform: scaleY(0.5);
			}

			.steps__text-column-item-text {
				margin: 0 8px;

			}
		}
	}
</style>